package com.spdg.ring.xml;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import cn.wolf.xml.Parser;

import com.spdg.ring.entity.ContentEntity;
import com.spdg.ring.sqlite.model.ArticleList;

/**
 * <p>Title: 子项内容解析器       </p>
 * <p>Description: 
 *    <level title="福州绕圈圈" about="http://newapps.appscomb.cn/op/about_comb_cn?pt=turnfuzhou" duration="" shareenable="1" favoenable="1" favolistenable="1" __classid="142" slogan="玩乐速递 | 各类玩乐资讯及时放送" id=""> 
 *        <item 
 *            title="玩乐速递" 
 *            id="turn2_191" 
 *            sharelink="http://turnfuzhou.appscomb.cn/op/channel_2?id=191" 
 *            sharehead="福州绕圈圈" 
 *            src="http://newapps.appscomb.cn/op/turnfuzhou_content?id=191&time=&pt=turnfuzhou" 
 *            icon="http://newapps.appscomb.cn/thumbs/editor/appscomb5082c442e170e?size=114" 
 *            group="" 
 *            summary="这家店，隐秘于竹林境内，藏身于孝义巷中，为无数饕客所津津乐道，无地址、无门牌，草根般低调。即便百米之外就是车水马龙的城市核心，却一如门前那条长巷一般，......" 
 *            type="Item" 
 *            endnode="1" 
 *            name="酸甜福州》大隐隐于市 细巷里的竹林境秘店" 
 *            __p="" 
 *            date="2012-10-17 23:31:08"> 
 *        </item>
 *      </level>
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: HX Co., Ltd.          </p>
 * <p>Create Time: 2012-10-25             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class ContentParser extends Parser {

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.wolf.xml.Parser#traverse(org.w3c.dom.Element)
	 */
	@Override
	protected List<?> traverse(Element root) {
		List<ContentEntity> contentlist = new ArrayList<ContentEntity>();
		List<ArticleList> list = new ArrayList<ArticleList>();

		if (root == null) {
			return Collections.emptyList();
		}

		ContentEntity contentEntity = new ContentEntity();

		NamedNodeMap rootNamedNodeMap = root.getAttributes();
		if (rootNamedNodeMap != null) {
			Node rootTitleNode = rootNamedNodeMap.getNamedItem("title");
			if (rootTitleNode != null)
				contentEntity.setTitle(rootTitleNode.getNodeValue());

			Node rootAboutNode = rootNamedNodeMap.getNamedItem("about");
			if (rootAboutNode != null)
				contentEntity.setAbout(rootAboutNode.getNodeValue());

			Node rootSloganNode = rootNamedNodeMap.getNamedItem("slogan");
			if (rootSloganNode != null)
				contentEntity.setSlogan(rootSloganNode.getNodeValue());
		}

		NodeList nodelist = root.getChildNodes();

		for (int i = 0; i < nodelist.getLength(); i++) {
			Node node = nodelist.item(i);

			ArticleList entity = new ArticleList();

			NamedNodeMap namedNodeMap = node.getAttributes();

			if (namedNodeMap != null) {
				Node idNode = namedNodeMap.getNamedItem("id");
				if (idNode != null)
					entity.setIdName(idNode.getNodeValue());
				
				Node srcNode = namedNodeMap.getNamedItem("src");
				if (srcNode != null)
					entity.setSrc(srcNode.getNodeValue());

				Node sharelinkNode = namedNodeMap.getNamedItem("sharelink");
				if (sharelinkNode != null)
					entity.setSharelink(sharelinkNode.getNodeValue());

				Node shareheadNode = namedNodeMap.getNamedItem("sharehead");
				if (shareheadNode != null)
					entity.setSharehead(shareheadNode.getNodeValue());

				Node summaryNode = namedNodeMap.getNamedItem("summary");
				if (summaryNode != null)
					entity.setSummary(summaryNode.getNodeValue());

				Node typeNode = namedNodeMap.getNamedItem("type");
				if (typeNode != null)
					entity.setType(typeNode.getNodeValue());

				Node nameNode = namedNodeMap.getNamedItem("name");
				if (nameNode != null)
					entity.setName(nameNode.getNodeValue());

				Node groupNode = namedNodeMap.getNamedItem("group");
				if (groupNode != null)
					entity.setGroup(groupNode.getNodeValue());

				Node dateNode = namedNodeMap.getNamedItem("date");
				if (dateNode != null)
					entity.setDate(dateNode.getNodeValue());

				Node iconNode = namedNodeMap.getNamedItem("icon");
				if (iconNode != null)
					entity.setIcon(iconNode.getNodeValue());
				
				Node iconFileNode = namedNodeMap.getNamedItem("icon_file");
				if (iconFileNode != null)
					entity.setIconFile(iconFileNode.getNodeValue());
				
				Node fileNode = namedNodeMap.getNamedItem("file");
				if (fileNode != null)
					entity.setFile(fileNode.getNodeValue());
				
				Node titleNode = namedNodeMap.getNamedItem("title");
				if (titleNode != null)
					entity.setTitle(titleNode.getNodeValue());

				list.add(entity);
			}
		}

		contentEntity.setItemList(list);

		contentlist.add(contentEntity);
		return contentlist;
	}
}
